package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * Created by pchavez on 04/02/14.
 */
public class InputHandler {

    private BufferedReader bufferedReader;
    private UserManager userManager;

    public InputHandler(BufferedReader reader, UserManager userManager){
        this.bufferedReader = reader;
        this.userManager = userManager;
    }

    public String getInput() {
        try {
            String input = bufferedReader.readLine();
            return input;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public void logInPrompt() {
        boolean logInSuccess = false;

        while(logInSuccess == false){
            System.out.println("Please, enter your user ID and password.");
            String userID = getInput();
            String password = getInput();

            logInSuccess = userManager.logIn(userID, password);
        }
    }

    public String mainMenu() {
        if(userManager.getCurrentUser() == null) {
            return "What would you like to do? (Check out/Check in/List books/List movies)";
        }else {
            return "What would you like to do? (Check out/Check in/List books/List movies/ See my information)";
        }
    }

    public void checkInPrompt(Repository repository) {
        Boolean checkInSuccess = false;
        while(checkInSuccess == false){
            System.out.println("What item do you want to return?");
            String title = getInput();
            checkInSuccess = repository.checkIn(title, userManager.getCurrentUser());
            if(checkInSuccess) {
                System.out.println("Thank you for returning it.");
            }else {
                System.out.println("That item is not a valid option to return.");
            }
        }
    }

    public void checkOutPrompt(Repository repository) {
        Boolean checkOutSuccess = false;
        while(checkOutSuccess == false){
            System.out.println("What item do you want to check out?");
            String title = getInput();
            checkOutSuccess = repository.checkOut(title, userManager.getCurrentUser());
            if(checkOutSuccess){
                System.out.println("Thank you, enjoy it.");
            }else {
            System.out.println("Not available.");
            }
        }
    }
}
